﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DialogMgr : BaseManager<DialogMgr>
{
    List<Dialog> _allDlg = new List<Dialog>();
    
    public T Open<T>()where T : Dialog, new()
    {
        var dlg = new T();
        _allDlg.Add(dlg);
        return dlg;
    } 
    public void Close(Dialog dlg)
    {
        dlg.CloseSelf();
        _allDlg.Remove(dlg);
    }
    public void CloseAll()
    {
        foreach(var dlg in _allDlg)
        {
            dlg.CloseSelf();
        }
        _allDlg.Clear();
    }
}

public abstract class Dialog
{
    protected GameObject _root;
    
    public bool IsAlive { get { return _root != null; } }
    protected void load(string uiPath)
    {
        _root = UIMgr.GetInstance().Add(uiPath);
        var btnClose = _root.Find<Button>("BtnClose");
        if(btnClose != null)
        {
            btnClose.onClick.AddListener(onClose);
        }
    }
    public virtual void CloseSelf()
    {
        UIMgr.GetInstance().Remove(_root);
    }

    protected virtual void onClose()
    {
        CloseSelf();
        
    }
}
